package co.yixiang.modules.system.rest;

import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.Partner;
import co.yixiang.modules.system.dto.PartnerDto;
import co.yixiang.modules.system.dto.queryCriteria.PartnerQueryCriteria;
import co.yixiang.modules.system.service.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author zf
* @date 2021-10-09
*/
@AllArgsConstructor
@Api(tags = "合伙人记录管理")
@RestController
@RequestMapping("/api/partner")
public class PartnerController {

    private final PartnerService partnerService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','partner:list')")
    public void download(HttpServletResponse response, PartnerQueryCriteria criteria) throws IOException {
        partnerService.download(Convert.toList(PartnerDto.class, partnerService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询合伙人记录")
    @ApiOperation("查询合伙人记录")
    @PreAuthorize("@el.check('admin','partner:list')")
    public ResponseEntity<Object> getPartners(PartnerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(partnerService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增合伙人记录")
    @ApiOperation("新增合伙人记录")
    @PreAuthorize("@el.check('admin','partner:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Partner resources){
        return new ResponseEntity<>(partnerService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改合伙人记录")
    @ApiOperation("修改合伙人记录")
    @PreAuthorize("@el.check('admin','partner:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Partner resources){
//        partnerService.updateById(resources);
        partnerService.check(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除合伙人记录")
    @ApiOperation("删除合伙人记录")
    @PreAuthorize("@el.check('admin','partner:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        partnerService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}